<?
include_once 'CoreModel.php';
Class ImportSalesReportModel extends CoreModel {
	private $table;
	public function __construct() {
		$this -> table = "salesrevenue";
		parent::__construct();
	}

	public function insertConnectData($result) {
		/* $result is result from sap */
		//	print_r($result);
		//exit();
		// default log message
		$data_log['log_function'] = "ZSDRFC_SALESREVENUE";
		$data_log['log_table'] = "IT_DATA";
		$data_log['log_record'] = 0;
		$data_log['log_type'] = "INSERT";
		$data_log['log_status'] = "COMPLETED";
		$data_log['log_by_name'] = "Batch Job";
		$data_log['log_page'] = "adminpanel/importsalesreport";

		$numRows = 0;
		// start begin trans
		$this -> db -> trans_begin();
		$this -> db -> query('delete from salesrevenue');
		$this->db->query("SET NAMES tis620"); 
		$dataInsert = array();
		
		foreach ($result["IT_DATA"] as $srpt) {
			$data = array();
			$data['VBELN'] = $srpt["VBELN"];
			$data['POSNR'] = $srpt["POSNR"];
			$data['FKART'] = $srpt["FKART"];
			$data['FKDAT'] = $srpt["FKDAT"];
			$data['KUNAG'] = $srpt["KUNAG"];
			$data['FKSTO'] = $srpt["FKSTO"];
			$data['FKIMG'] = $srpt["FKIMG"];
			$data['VRKME'] = $srpt["VRKME"];
			$data['MEINS'] = $srpt["MEINS"];
			$data['FKLMG'] = $srpt["FKLMG"];
			$data['NETWR'] = $srpt["NETWR"];
			$data['MATNR'] = $srpt["MATNR"];
			$data['PSTYV'] = $srpt["PSTYV"];
			$data['MWSBP'] = $srpt["MWSBP"];
			$data['PERNR'] = $srpt["PERNR"];
			$data['PRICE'] = $srpt["PRICE"];
			$data['FKART_T'] = $srpt["FKART_T"];
			$data['KUNNR_T'] = $srpt["KUNNR_T"];
			$data['KONDA_T'] = $srpt["KONDA_T"];
			$data['KDGRP_T'] = $srpt["KDGRP_T"];
			$data['KVGR1_T'] = $srpt["KVGR1_T"];
			$data['KVGR2_T'] = $srpt["KVGR2_T"];
			$data['KVGR3_T'] = $srpt["KVGR3_T"];
			$data['KVGR4_T'] = $srpt["KVGR4_T"];
			$data['KVGR5_T'] = $srpt["KVGR5_T"];
			$data['KVGR4_KT'] = $srpt["KVGR4_KT"];
			$data['KVGR5_KT'] = $srpt["KVGR5_KT"];
			$data['PERNR_T'] = $srpt["PERNR_T"];
			$data['VKGRP_T'] = $srpt["VKGRP_T"];
			$data['MATNR_T'] = $srpt["MATNR_T"];
			$data['MATKL_T'] = $srpt["MATKL_T"];
			$data['PRODH1_T'] = $srpt["PRODH1_T"];
			$data['PRODH2_T'] = $srpt["PRODH2_T"];
			$data['PRODH3_T'] = $srpt["PRODH3_T"];
			$dataInsert[] = $data;
		}

		// insert into SALES_DATA;
		$this -> db -> insert_batch($this -> table, $dataInsert);

		if ($this -> db -> trans_status() === FALSE) {
			// if error on tranaction
			$this -> db -> trans_rollback();
				
			// insert log  table & page
			$this->insertLogFailed('ZSDRFC_SALESREVENUE', 'IT_DATA', 'adminpanel/importsalesreport');
			
			return array("flag" => false, "message" => "error on transfer");
		} else {
			// when complete
			$this -> db -> trans_commit();
			$this->insertLogComplete('ZSDRFC_SALESREVENUE', 'IT_DATA', count($dataInsert), 'adminpanel/importsalesreport');
			return array("flag" => true, "connect_row" => count($dataInsert));
		}
	}

}
?>